<template>
  <div class="app-container">
      <div class="score-container">
          <div class="scoreUserTitle">
              当前用户积分：<a href="#" @click="getExchangeRecord" style="cursor: pointer;">{{ userScore }}</a>
          </div>
          <div class="scoreList">
              <div class="scoreItem shadowHover" v-for="(item, index) in goodList">
                  <el-image class="goods_img" :src="baseUrl+item.img"></el-image>
                  <div class="scoreTitle beyond_ellipsis2">{{ item.name }}</div>
                  <div class="dis_price">{{ item.price }}<span>积分</span></div>
                  <div class="goods_btn" @click="exchangeConfirm(item)" v-hasPermi="['partybuilding:MallOrder:add']" v-if="item.quantity>0">立即兑换</div>
                  <div class="goods_btn_hs" v-else>暂停兑换</div>
              </div>
              <div class="splitpage">
                  <pagination
                      v-show="total>0"
                      :total="total"
                      v-model:page="queryParams.pageNum"
                      v-model:limit="queryParams.pageSize"
                      @pagination="getList"
                  />
              </div>
          </div>
      </div>
    
    <el-dialog title="查看兑换记录" v-model="open" width="41%" append-to-body>
        <el-row>
            <el-table v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">
                <el-table-column label="兑换时间" align="left" prop="updateTime" width="250"/>
                <el-table-column label="兑换积分" align="left" prop="points" min-width="250"/>
                <el-table-column label="备注" align="left" prop="remark" width="250"/>
            </el-table>
            <pagination
                v-show="exchangeTotal>0"
                :total="exchangeTotal"
                v-model:page="queryExchangeParams.pageNum"
                v-model:limit="queryExchangeParams.pageSize"
                @pagination="getExchangeRecord"
            />
        </el-row>
        <template #footer>
          <div class="dialog-footer" style="margin-top: 19px;">
            <el-button @click="cancelView">取 消</el-button>
          </div>
        </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScoreExchange">
import { listPoints, topPoints } from "@/api/mall/MallPoint";
import { listMallGoods } from "@/api/mall/GoodsManage";
import { addMallOrder } from "@/api/mall/GoodsOrder";
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const { proxy } = getCurrentInstance();
const loading = ref(true);
const data = reactive({
queryParams: {
  pageNum: 1,
  pageSize: 14
},
queryExchangeParams: {
  pageNum: 1,
  pageSize: 10
}
});
const { queryParams, queryExchangeParams } = toRefs(data);
const goodList = ref([]);
const total = ref(0);
const userScore = ref(0);
const open = ref(false);
const exchangeList = ref([]);
const exchangeTotal = ref(0);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listMallGoods(queryParams.value).then(response => {
      goodList.value = response.rows;
      total.value = response.total;
      loading.value = false;
  });
}
/** 查询用户积分 */
function getUserScore() {
  topPoints().then(response => {
      let points = response.data;
      userScore.value = points.total==null?0:points.total;
  });
}
/** 兑换确认弹窗 */
function exchangeConfirm(row) {
  let id = row.id;
  let price = row.price;
  if (userScore.value<price){
      proxy.$modal.msgWarning("积分不足，无法兑换");
  }else{
      proxy.$modal.confirm('确认兑换商品名称为"' + row.name + '"？').then(function() {
          return exchange(row);
      }).then(() => {
        setTimeout(() => {
          getList();
          getUserScore();
        }, 100);
        proxy.$modal.msgSuccess("兑换商品成功");
      }).catch(() => {});
  }
}
/** 兑换商品 */
function exchange(row) {
  let obj = {};
  obj.goodsId = row.id; //商品Id
  obj.quantity = 1; //购买数量
  obj.totalPrice = row.price; //总价
  addMallOrder(obj);
}
/** 查询积分兑换记录 */
function getExchangeRecord() {
  listPoints(queryExchangeParams.value).then(response => {
      exchangeList.value = response.rows;
      exchangeTotal.value = response.total;
      if (open.value === false) {
          open.value = true;
      }
  });
}

/** 关闭查看兑换记录弹窗 */
function cancelView() {
  open.value = false;
}

getList();
getUserScore();
</script>
<style lang='scss' scoped>
.app-container {
    background-color: #ede5e5!important;
}
.score-container {
  width: 90%;
  margin: 0 auto;
  font: 12px / 1.5 punctuation, 微软雅黑;
  color: #333;
}
.score-container .scoreUserTitle {
  color: #b3201e;
  font: 18px / 1.5 punctuation, 微软雅黑;
  margin-left: 10px;
  a:hover {
      text-decoration: underline;
      color: #e90e0b;
  }
}
.score-container .scoreList {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  .scoreItem {
      width: 190px;
      height: 300px;
      padding: 10px;
      margin: 10px 7px;
      border-radius: 4px;
      cursor: pointer;
      background: #ffffff;
      position: relative;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .goods_img {
          width: 170px;
          height: 170px;
          text-align: center;
      }
      .scoreTitle {
          margin-top: 4px;
          margin-bottom: 4px;
          font-size: 14px;
          color: #4a4343;
          height: 40px;
          overflow: hidden;
          line-height: 19px;
      }
      .beyond_ellipsis2 {
          overflow: hidden;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          -moz-line-clamp: 2;
          line-clamp: 2;
      }
      .dis_price {
          color: #ad1e1d;
          font-size: 18px;
          span {
              font-size: 12px;
              margin-left: 2px;
          }
      }
      .ori_price {
          font-size: 16px;
          text-decoration: line-through;
          color: #c4c0c0;
      }
      .goods_btn_hs {
          width: 174px;
          height: 28px;
          line-height: 28px;
          font-size: 13px;
          text-align: center;
          cursor: pointer;
          margin: 0 auto;
          color: #fff;
          border-radius: 15px;
          position: relative;
          top: 5px;
          background: linear-gradient(297deg, #ccc, rgb(143, 139, 139) 48%, rgb(122, 121, 121));
      }
      .goods_btn {
          width: 174px;
          height: 28px;
          line-height: 28px;
          font-size: 13px;
          text-align: center;
          cursor: pointer;
          margin: 0 auto;
          color: #fff;
          border-radius: 15px;
          position: relative;
          top: 5px;
          background: linear-gradient(297deg, #ad1e1d, #b3201e 48%, #b3201e);
      }
  }
  .shadowHover:hover {
      box-shadow: 0 1px 10px 0 rgba(0, 0, 0, .5);
  }
}

.splitpage {
  width: 100%;
  text-align: right;
  position: relative;
  top: -19px;
  .pagination-container {
      background-color: #ede5e5;
  }
}
</style>